package com.shoujifeng.snowmusic.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.geniuseoe2012.lazyloaderdemo.cache.ImageLoader;
import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.player.R.drawable;

public class LoaderAdapter extends BaseAdapter {

	private static final String TAG = "LoaderAdapter";
	private boolean mBusy = false;

	public void setFlagBusy(boolean busy) {
		this.mBusy = busy;
	}

	private ImageLoader mImageLoader;
	private Context mContext;
	private int lay; // layout�ļ�
	private int[] key; // �ؼ���id
	private String[] keys; // �ؼ���Ӧmap��key
	private ArrayList<HashMap<String, Object>> array; // list
	private int background; // û�м��ص�ͼƬʱ��ͼƬ
	private String[] dbkeys; // ͼƬ��key
	private int[] dbids; // ͼƬ��id

	public LoaderAdapter(Context context,
			ArrayList<HashMap<String, Object>> array, 
			int[] key, String keys[],String[] dbkeys, int[] dbids, int background,int lay) {
		this.mContext = context;
		this.lay = lay;
		this.array = array;
		this.key = key;
		this.keys = keys;
		this.background = background;
		this.dbkeys = dbkeys;
		this.dbids = dbids;
		mImageLoader = new ImageLoader(context);
	}

	public ImageLoader getImageLoader() {
		return mImageLoader;
	}

	@Override
	public int getCount() {
		return array.size();
	}

	@Override
	public HashMap<String, Object> getItem(int position) {
		return array.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		
		if (convertView == null ) {
			convertView = LayoutInflater.from(mContext).inflate(lay, null);
		}

		HashMap<String, Object> map = getItem(position);

		for (int i = 0; i < keys.length; i++) {
			TextView text = (TextView) convertView.findViewById(key[i]);
			text.setText(map.get(keys[i]).toString());
		}

		for (int i = 0; i < dbkeys.length; i++) {
			
			ImageView iv = (ImageView) convertView.findViewById(dbids[i]);
			iv.setImageResource(background);
			mImageLoader.DisplayImage(map.get(dbkeys[i]).toString(),iv, false,background);
		}
		
		return convertView;
	}

	static class ViewHolder {
		static TextView mTextView;
		static ImageView mImageView;
	}

}
